package com.example.hyznbk.view.widget;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.provider.Settings;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.hyznbk.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by tangminglong on 17/7/18.
 * 加载、空布局
 */

public class EmptyMainLayout extends FrameLayout{

    public static final int STATUS_HIDE = 1001;
    public static final int STATUS_LOADING = 1;
    public static final int STATUS_NO_NET = 2;
    public static final int STATUS_NO_DATA = 3;

    private Context mContext;
    private int mBgColor;

    private FrameLayout mEmptyLayout;
    private LinearLayout linearBtn;

    private TextView tvException;
    private GifView ivExceptionGif;
    private ImageView ivExceptionPng;
    private TextView btnRefresh;
    private TextView btnCheckNet;

    private int mEmptyStatus = STATUS_LOADING;
    private OnRetryListener mOnRetryListener;


    public EmptyMainLayout(@NonNull Context context) {
        this(context,null);
    }

    public EmptyMainLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.ggfx_emptyLayout);
        try {
            mBgColor = a.getColor(R.styleable.ggfx_emptyLayout_ggfx_background_color, Color.WHITE);
        } finally {
            a.recycle();
        }
        View.inflate(mContext,R.layout.ggfx_include_exceptionpage,this);
        mEmptyLayout = (FrameLayout) findViewById(R.id.exception_page);
        linearBtn = (LinearLayout) findViewById(R.id.linear_btn);
        tvException = (TextView) findViewById(R.id.tv_exception);
        ivExceptionGif = (GifView) findViewById(R.id.iv_exception_gif);
        ivExceptionPng = (ImageView) findViewById(R.id.iv_exception_png);
        btnRefresh = (TextView) findViewById(R.id.btn_refresh);
        btnCheckNet = (TextView) findViewById(R.id.btn_checknet);
        btnRefresh.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
              if (mOnRetryListener != null){
                  mOnRetryListener.onRetry();
              }
            }
        });
        btnCheckNet.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                mContext.startActivity(intent);
            }
        });
        mEmptyLayout.setBackgroundColor(mBgColor);
        switchEmptyView();
    }
    /**
     * 设置重试监听器
     *
     * @param retryListener 监听器
     */
    public void setRetryListener(OnRetryListener retryListener) {
        this.mOnRetryListener = retryListener;
    }


    /**
     * 隐藏视图
     */
    public void hide() {
        mEmptyStatus = STATUS_HIDE;
        switchEmptyView();
    }



    /**
     * 设置状态
     *
     * @param emptyStatus
     */
    public void setEmptyStatus(@EmptyStatus int emptyStatus) {
        mEmptyStatus = emptyStatus;
        switchEmptyView();
    }

    /**
     * 获取状态
     * @return  状态
     */
    public int getEmptyStatus() {
        return mEmptyStatus;
    }

    /**
     * 设置异常消息
     *
     * @param msg 显示消息
     */
    public void setEmptyMessage(String msg) {
        tvException.setText(msg);
    }

   /* public void hideErrorIcon() {
        mTvEmptyMessage.setCompoundDrawables(null, null, null, null);
    }
    */
    private void switchEmptyView(){
        switch (mEmptyStatus){
            case STATUS_LOADING:
                setVisibility(VISIBLE);
                linearBtn.setVisibility(View.GONE);
                //Glide.with(this).load(R.drawable.ggfx_loading_view_icon).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv_exception);
                ivExceptionGif.setVisibility(View.VISIBLE);
                ivExceptionPng.setVisibility(View.GONE);
                ivExceptionGif.setMovieResource(R.drawable.loading_view_icon);
                tvException.setText("努力加载中...");
                break;
            case STATUS_NO_DATA:
                setVisibility(VISIBLE);
                linearBtn.setVisibility(View.GONE);
                ivExceptionGif.setVisibility(View.GONE);
                ivExceptionPng.setVisibility(View.VISIBLE);
                ivExceptionPng.setImageResource(R.mipmap.img_load_nodata);
                tvException.setText("暂无数据");
                break;
            case STATUS_NO_NET:
                setVisibility(VISIBLE);
                linearBtn.setVisibility(View.VISIBLE);
                ivExceptionPng.setVisibility(View.VISIBLE);
                ivExceptionGif.setVisibility(View.GONE);
                ivExceptionPng.setImageResource(R.mipmap.img_load_failed);
                tvException.setText("糟糕！出错了");
                break;
            case STATUS_HIDE:
                setVisibility(GONE);
                break;
        }
    }


    public GifView getGifView(){
        return ivExceptionGif;
    }

    /**
     * 点击重试监听器
     */
    public interface OnRetryListener {
        void onRetry();
    }

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({STATUS_LOADING, STATUS_NO_NET, STATUS_NO_DATA})
    public @interface EmptyStatus{}
}
